<?php


namespace App\Services;

/**
 * 单例模式IOC容器 可当全局变量使用
 */
class IOC
{
    protected $attributes=[];
    protected static $instance;
    protected function __construct(){}
    protected function __clone(){}

    public static function getInstance() :self
    {
        if(!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function setAttribute($key, $value) :self {
        $this->attributes[$key] = $value;
        return $this;
    }

    public function getAttribute($key){
        if(array_key_exists($key, $this->attributes)){
            return $this->attributes[$key];
        }
        return null;
    }
}
